namespace CGF
{
    public partial class PanelMyTraining : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<PanelMyTraining>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelMyTraining>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelMyTraining", layer, isFloat);
			BindProperties(transform);
            
            BtnBack.SetClick(Close);

            TextTotalScore.text = "";

            // "id":363313398496104448,
            // "user_id":359060771344224256,
            // "type":1,
            // "product_id":0,
            // "shop_order_id":363313398491910144,
            // "url":"",
            // "reward":2000,
            // "state":3,
            // "reason":"buhaokan ",
            // "created_at":"2025-09-29T02:17:44.238-03:00",
            // "updated_at":"2025-09-29T02:20:56.354-03:00"
            int state = 3; //1待审 2拒绝 3通过
            string api = "/v1/reward/task/my?state=" + state;
            string listKey = "myTasks";
            Table.Column[] columns =
            {
                (cell, data) => TimeHelper.Parse2Date(data["created_at"].ToString(), false),
                (cell, data) => data["id"].ToString(),
                (cell, data) => StringHelper.Format2Decimal((int)data["reward"] * 0.01f),
                (cell, data) => I18N_view,
            };
            Table.GetComponent<Table>().ReqData(api, listKey, columns, null, null, data =>
            {
                PanelMyTrainingDetail.Open(data);
            });

            EventManager.Add<Event_UserInfoUpdate>(OnUserInfoUpdate);
            OnUserInfoUpdate(new Event_UserInfoUpdate());
        }
        
        private void OnUserInfoUpdate(Event_UserInfoUpdate e)
        {
            TextTotalScore.text = $"{I18N_total_score} {StringHelper.Format2Decimal((int)UserInfo.Info["balance"] * 0.01f)}";
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            EventManager.Remove<Event_UserInfoUpdate>(OnUserInfoUpdate);
        }

    }
}
